package com.matrix.draft.juc.day7;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class TestAtomic {
    public static void main(String[] args) throws Exception {
        AtomicReference<Integer> ref = new AtomicReference<>(0);
        CountDownLatch latch = new CountDownLatch(2);

        new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                ref.getAndUpdate(prev -> prev + 1);
            }
            latch.countDown();
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                ref.getAndUpdate(prev -> prev - 1);
            }
            latch.countDown();
        }).start();

        latch.await();
        System.out.println(ref.get());

    }
}
